package cn.dkk.behavior_design.meditor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author dkk
 * @date 2022/7/28
 * @desc 抽象中介者
 */
public class Mediator {

    private List<Mediable> consumers = new ArrayList<>();

    private List<Mediable> producers = new ArrayList<>();

    private BlockingDeque<String> mesQueue=new LinkedBlockingDeque<>();

    public void reg(Mediable mediable){
        if (mediable instanceof Consumer){
            consumers.add(mediable);
        }else{
            producers.add(mediable);
        }
    }

    public void deReg(Mediable mediable) {
        if (mediable instanceof Consumer) {
            consumers.remove(mediable);
        } else {
            producers.remove(mediable);
        }
    }

    public String getMes(){
        return mesQueue.pollLast();
    }

    public void sendMes(String mes){
        mesQueue.offerFirst(mes);
    }

}
